package com.yuncheng.spcyApi.schedule;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.partyService.PartyHyService;
import com.yuncheng.spcyApi.service.IPartyRsDfjnjlService;
import com.yuncheng.spcyApi.service.ISpcyOaGzxkService;
import com.yuncheng.spcyApi.spcyService.SqsxQywjclService;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Configuration
public class ActiveScheduleTask {

    @Resource
    @Lazy
    private ISpcyOaGzxkService spcyOaGzxkService;

    @Resource
    @Lazy
    private SqsxQywjclService sqsxQywjclService;

    @Resource
    @Lazy
    private IPartyRsDfjnjlService partyRsDfjnjlService;

    @Resource
    @Lazy
    private PartyHyService partyHyService;

    /**
     * 验证工资项是否存在,不存在就新增
     */
    @Async("asyncNormalTask")
    public void verifyAddGzxk(String mc){
        if (StringUtils.isBlank(mc)){
            return;
        }

        // 验证该项是否存在
        List<SpcyOaGzxk> existList = spcyOaGzxkService.list(new LambdaQueryWrapper<SpcyOaGzxk>()
                .eq(SpcyOaGzxk::getFmc, mc)
                .eq(SpcyOaGzxk::getFyxzt, "0"));
        if (CollectionUtil.isEmpty(existList)){
            // 不存在
            List<SpcyOaGzxk> sizeList = spcyOaGzxkService.list();
            int count = sizeList.size() + 1;
            SpcyOaGzxk gzxk = new SpcyOaGzxk();
            gzxk.setId(GetUuIdUtils.ReplaceUuId());
            gzxk.setForder(count);
            gzxk.setFyxzt("0");
            gzxk.setFmc(mc);
            spcyOaGzxkService.save(gzxk);
        }

    }


    @Async("asyncNormalTask")
    public void tqZwxtFileToLocalFile(List<SpcySqsxQywjcl> qywjclList){
        sqsxQywjclService.tqZwxtFileToLocalFile(qywjclList);
    }

    @Async("asyncNormalTask")
    public void initPartyDfjl(String gzx, String gxzz, SpcyOaGzjl gzjl){
        if (gzjl == null){
            return;
        }
        if (StringUtils.isBlank(gzjl.getFuserid())){
            return;
        }
        if (StringUtils.isBlank(gxzz)){
            return;
        }
        BigDecimal sfgzValue = new BigDecimal(gxzz);
        BigDecimal bfbValue = new BigDecimal("0.01");
        BigDecimal dfValue = sfgzValue.multiply(bfbValue);

        String yjje = dfValue.toString(); // 应缴金额

        PartyRsDfjnjl dfjnjl = new PartyRsDfjnjl();
        dfjnjl.setId(GetUuIdUtils.ReplaceUuId());
        dfjnjl.setFjfry(gzjl.getFxm());
        dfjnjl.setFuserid(gzjl.getFuserid());
        dfjnjl.setFyjje(yjje);

        /** 获取当前月份的最后一天 */
        Date date = new Date();
        int year = DateUtil.year(date);//年份
        String month = DateConversionUtils.DateToIntegerMonth(date); // 月份
        //yyyyMM----->yyyyMMdd
        String yearMonth = year + month + "01"; // year可写死，月份第一天
        DateTime dateTime = DateUtil.parse(yearMonth, DatePattern.PURE_DATE_PATTERN);//转为Date类型
        DateTime ofMonth = DateUtil.endOfMonth(dateTime); //获取月份最后一天（带时分秒）
        DateTime truncate = DateUtil.truncate(ofMonth, DateField.DAY_OF_MONTH);//去时分秒

        dfjnjl.setFyjsj(DateConversionUtils.DateToStringYYYYMMDD(truncate));

        partyRsDfjnjlService.initAddDfjl(dfjnjl);
    }

    @Async("asyncNormalTask")
    public void syncHyjl(PartyRsDwhyjh jh, String fhyjhid){
        partyHyService.syncHyjl(jh, fhyjhid);
    }
}
